package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 作者:hfj
 * 功能:
 * 日期: 2024/4/18 14:52
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {

    //用户角色关系
    @Autowired
    private AdminRoleService adminRoleService;

    //角色条件分页查询
    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {

        //1.获取条件值
        String roleName = roleQueryVo.getRoleName();

        //创建mp条件对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //2.判断是否为空,不为封装条件查询
        if (!StringUtils.isEmpty(roleName)){
            wrapper.like(Role::getRoleName,roleName);
        }

        //3.调用方法实现条件分页查询
        IPage<Role> rolePage = baseMapper.selectPage(pageParam, wrapper);

        //返回分页对象
        return rolePage;
    }

    //获取所有角色 根据用户查询用户分配角色列表
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //1.查询所有角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //2.根据id查询用户分配角色列表
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);

        List<Long> roleIdsList = adminRoleList.stream().map(c->c.getRoleId()).collect(Collectors.toList());

        //对角色进行分类
        List<Role> assignRoleList = new ArrayList<Role>();
        for (Role role : allRolesList) {
            //已分配
            if(roleIdsList.contains(role.getId())) {
                assignRoleList.add(role);
            }
        }

        //封装到map,返回
        Map<String, Object> result = new HashMap<>();
        result.put("allRolesList",allRolesList);
        result.put("assignRoles",assignRoleList);
        return result;
    }

    //为用户进行角色分配
    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        //1.删除用户已经分配过的角色数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        adminRoleService.remove(wrapper);

        //2.重新分配
        //for (Long roleId : roleIds){
        //    AdminRole adminRole = new AdminRole();
        //    adminRole.setAdminId(adminId);
        //    adminRole.setRoleId(roleId);
        //    adminRoleService.save(adminRole);
        //}

        //2.重新分配
        ArrayList<AdminRole> list = new ArrayList<>();
        for (Long roleId:roleIds){
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);

            //放到list集合
            list.add(adminRole);
        }

        //调用方法
        adminRoleService.saveBatch(list);
    }
}
